<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

class ListTable
{
	static $id_element = 1;
	
	private $title;
	private $has_numbers;
	private $structure = array();
	
	function ListTable($title, $has_numbers=false)
	{
		$this->title = $title;
		$this->has_numbers = $has_numbers;
	}
	
	function AddLine($field_l, $value, $field_l_style=null, $field_r=null, $field_r_style=null)
	{
		$this->structure[$field_l] = array(
			'field_l_style' => $field_l_style,
			'value' => $value,
			'field_r' => $field_r,
			'field_r_style' => $field_r_style,
		);
	}
	
	function GetTable($sort_it = false)
	{
		if ( count($this->structure) < 1 )
		{
			return null;
		}
		
		$string = null;
		
		$string .= "<table cellspacing='0' class='listtable'>";
		$string .= "<thead>";
		$string .= "<tr>";
		$string .= "<td class='header_topleft'></td>";
		$string .= "<td colspan='2' class='header_top'></td>";
		$string .= "<td class='header_topright'></td>";
		$string .= "</tr>";
		$string .= "<tr>";
		$string .= "<td class='t_left'></td>";
		$string .= "<td class='header_left'>".$this->title."</td>";
		$string .= "<td class='header_right'></td>";
		$string .= "<td class='t_right'></td>";
		$string .= "</tr>";
		$string .= "</thead>";
		$string .= "<tfoot>";
		$string .= "<tr>";
		$string .= "<td class='header_bottomleft'></td>";
		$string .= "<td colspan='2' class='header_bottom'></td>";
		$string .= "<td class='header_bottomright'></td>";
		$string .= "</tr>";
		$string .= "</tfoot>";
		$string .= "<tbody>";
		
		$i = 1;
		
		foreach ($this->structure as $field => $v)
		{
			$string .= "<tr>";
			$string .= "<td class='t_left'></td>";
			$string .= "<td class='left'>";
			
			if ($v['field_r'] != null)
			{
				$string .= "<span style='float: right; margin-right: 5px; ".$v['field_r_style']."'>";
				$string .= $v['field_r'];
				$string .= "</span>";
			}
			
			$string .= "<span style='".$v['field_l_style']."'>";
			$string .= ( $this->has_numbers ? $i.". " : null ).$field;
			$string .= "</span>";
			$string .= "</td>";
			$string .= "<td class='right'>".$v['value']."</td>";
			$string .= "<td class='t_right'></td>";
			$string .= "</tr>";
			
			$i++;
		}
		
		$string .= "</tbody>";
		$string .= "</table>";
		
		return $string;
	}
}

?>